package com.waa.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.C;
import com.waa.gulimall.common.exception.BizCodeEnume;
import com.waa.gulimall.member.exception.EmailExsitException;
import com.waa.gulimall.member.exception.UsernameExsitException;
import com.waa.gulimall.member.vo.GiteeAccessToken;
import com.waa.gulimall.member.vo.MemberLoginVo;
import com.waa.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.waa.gulimall.member.entity.MemberEntity;
import com.waa.gulimall.member.service.MemberService;
import com.waa.gulimall.common.utils.PageUtils;
import com.waa.gulimall.common.utils.R;


/**
 * 会员
 *
 * @author waa
 * @email 2460335742@qq.com
 * @date 2022-02-22 10:23:31
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/member/list")
    public R memberlist() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("vip");
        return R.ok().put("member", Arrays.asList(memberEntity));
    }

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memberRegistVo) {
        try {
            memberService.regist(memberRegistVo);
        } catch (EmailExsitException e) {
            R.error(BizCodeEnume.EMAIL_EXIST_EXCEPTION.getCode(), BizCodeEnume.EMAIL_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExsitException e) {
            R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }
    @PostMapping("/oauth/login")
    public R login(@RequestBody GiteeAccessToken giteeAccessToken) {
        MemberEntity memberEntity = memberService.login(giteeAccessToken);
        if (null!=memberEntity){
            return R.ok().setData(memberEntity);
        }else {
            return R.error(BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (null!=memberEntity){
           return R.ok().setData(memberEntity);
        }else {
            return R.error(BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnume.LOGIN_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
